interface List {
    readonly id: number,
    name: string,
    age?: number
}
interface Result {
    data: List[]
}
function render (result: Result) {
    result.data.forEach((value) => {
        console.log(value.id, value.name);
        if (value.age) {
            console.log(value.age);
        }
    });
}
let result = {
    data: [{
        id: 1, name: 'A', sex: 'male'
    }, {
        id: 2, name: 'B'
    }
    ]
};
//鸭式变形法，如果用一个字面量定义一个对象，只要这个对象符合接口中必要的字段就可以被认为符合接口定义。
render(result);

render({
    data: [{
        id: 1, name: 'A', sex: 'male'
    }, {
        id: 2, name: 'B'
    }]
} as Result);

render(<Result>{
    data: [{
        id: 1, name: 'A', sex: 'male'
    }, {
        id: 2, name: 'B'
    }]
});

interface StringArray {
    [index: number]: string
}
let charts: StringArray = ['a', 'b'];

interface Names {
    [index: string]: string
}
let add2: (x: number, y: number) => number;

interface add3 {
    (x: number, y: number): number;
}
interface add4 {
    (): void;
}
//以上两种定义函数接口是等效的
//还有一种类型别名的定义方式

type Add4 = (x: number, y: number) => number;
let add4: Add4 = (a, b) => a + b;
console.log(add4(1, 2));

//混合类型的接口

interface Lib {
    (): void;
    version: string;
    doSomething (): void;
}

function getLib () {
    let lib: Lib = (() => { }) as Lib;
    lib.version = '1.0';
    lib.doSomething = () => { };
    return lib;
}

let lib1 = getLib();
lib1();
lib1.doSomething();
let lib2 = getLib();